import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final FocusNode _focusNode1 = FocusNode();
  final FocusNode _focusNode2 = FocusNode();
  bool _isFocused1 = false;
  bool _isFocused2 = false;

  @override
  void dispose() {
    _focusNode1.dispose();
    _focusNode2.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Focus Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Focus(
              focusNode: _focusNode1,
              onFocusChange: (hasFocus) {
                setState(() {
                  _isFocused1 = hasFocus;
                });
              },
              child: RawKeyboardListener(child: Container(
                width: 200,
                height: 50,
                color: _isFocused1 ? Colors.blue : Colors.grey,
                child: Center(
                  child: Text('Text Field 1'),
                ),
              ),
                  focusNode: FocusNode(),
              onKey: (RawKeyEvent event){
                RawKeyEventDataAndroid rawKeyEventDataAndroid =
                event.data as RawKeyEventDataAndroid;
                print(rawKeyEventDataAndroid.keyCode);
              },)
            ),
            SizedBox(height: 20),
            Focus(
              focusNode: _focusNode2,
              onFocusChange: (hasFocus) {
                setState(() {
                  _isFocused2 = hasFocus;
                });
              },
              child: Container(
                width: 200,
                height: 50,
                color: _isFocused2 ? Colors.blue : Colors.grey,
                child: Center(
                  child: Text('Text Field 2'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
